﻿namespace com.stifel.GlobalDao
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    using com.stifel.SharePoint;

    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Linq;

    using Context = GlobalDataContext;

    /// <summary>
    /// This accesses Timezone which is a site collection level list.
    /// </summary>
    public class TimezoneDao : BaseDao
    {
        #region Fields

        public const string ListName = "timezones";

        #endregion Fields

        #region Constructors

        public TimezoneDao( )
            : base(ListName)
        {
            // set to the site collection url
            CurrentWebUrl = SPContext.Current.Site.RootWeb.Url;
        }

        public TimezoneDao( string currentWebUrl )
            : base(ListName)
        {
            // set to the site collection url
            using ( SPSite spSite = new SPSite( currentWebUrl ) )
            {
                SPWeb spWeb = spSite.RootWeb;
                {
                    CurrentWebUrl = spWeb.Url;
                }
            }
        }

        #endregion Constructors

        #region Methods

        public void Add( Timezone value )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<Timezone> l = c.GetList<Timezone>( ListTitle );

                l.InsertOnSubmit( clean( value ) );
                c.SubmitChanges( );
            }
        }

        public void Clear( )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<Timezone> l = c.GetList<Timezone>( ListTitle );
                var v = ( from o in l
                          where o.Id > 0
                          select o );
                if ( v.Count( ) > 0 )
                {
                    l.DeleteAllOnSubmit( v );
                    c.SubmitChanges( );
                }
            }
        }

        public Timezone Get( string value )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<Timezone> l = c.GetList<Timezone>( ListTitle );
                Timezone v = ( from o in l
                               where o.TimeZoneID.Equals( value, StringComparison.OrdinalIgnoreCase )
                               select o ).SingleOrDefault( );
                return v;
            }
        }

        public Timezone Get( int id )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<Timezone> l = c.GetList<Timezone>( ListTitle );
                Timezone v = ( from o in l
                               where o.Id == id
                               select o ).SingleOrDefault( );
                return v;
            }
        }

        public Timezone Get( Guid uniqueId )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<Timezone> l = c.GetList<Timezone>( ListTitle );
                Timezone v = ( from o in l
                               where o.UniqueId == uniqueId
                               select o ).SingleOrDefault( );
                return v;
            }
        }

        public List<Timezone> GetAll( )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                List<Timezone> l = c.GetList<Timezone>( ListTitle ).ToList( );
                return l;
            }
        }

        Timezone clean( Timezone value )
        {
            return new Timezone
            {
                TimeZoneActive = value.TimeZoneActive,
                TimeZoneID = value.TimeZoneID,
                Title = value.Title
            };
        }

        #endregion Methods
    }

    /// <summary>
    /// This accesses USStates which is a site collection level list.
    /// </summary>
    public class USStatesDao : BaseDao
    {
        #region Fields

        public const string ListName = "usstates";

        #endregion Fields

        #region Constructors

        public USStatesDao( )
            : base(ListName)
        {
            // set to the site collection url
            CurrentWebUrl = SPContext.Current.Site.RootWeb.Url;
        }

        public USStatesDao( string currentWebUrl )
            : base(ListName)
        {
            // set to the site collection url
            using ( SPSite spSite = new SPSite( currentWebUrl ) )
            {
                SPWeb spWeb = spSite.RootWeb;
                {
                    CurrentWebUrl = spWeb.Url;
                }
            }
        }

        #endregion Constructors

        #region Methods

        public void Add( USStates value )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<USStates> l = c.GetList<USStates>( ListTitle );

                l.InsertOnSubmit( clean( value ) );
                c.SubmitChanges( );
            }
        }

        public void Clear( )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<USStates> l = c.GetList<USStates>( ListTitle );
                var v = ( from o in l
                          where o.Id > 0
                          select o );
                if ( v.Count( ) > 0 )
                {
                    l.DeleteAllOnSubmit( v );
                    c.SubmitChanges( );
                }
            }
        }

        public USStates Get( string abbreviation )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<USStates> l = c.GetList<USStates>( ListTitle );
                USStates v = ( from o in l
                               where o.USStateAbbreviation.StartsWith( abbreviation, StringComparison.OrdinalIgnoreCase )
                               select o ).SingleOrDefault( );
                return v;
            }
        }

        public USStates Get( int id )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<USStates> l = c.GetList<USStates>( ListTitle );
                USStates v = ( from o in l
                               where o.Id == id
                               select o ).SingleOrDefault( );
                return v;
            }
        }

        public USStates Get( Guid uniqueId )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<USStates> l = c.GetList<USStates>( ListTitle );
                USStates v = ( from o in l
                               where o.UniqueId == uniqueId
                               select o ).SingleOrDefault( );
                return v;
            }
        }

        public List<USStates> GetAll( )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                List<USStates> l = c.GetList<USStates>( ListTitle ).ToList( );
                return l;
            }
        }

        USStates clean( USStates value )
        {
            return new USStates
            {
                USStateAbbreviation = value.USStateAbbreviation,
                USStateActive = value.USStateActive,
                Title = value.Title
            };
        }

        #endregion Methods
    }
}